/**
 * 
 */
package com.tianqi.rms.sendFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.kit.HttpKit;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.tianqi.rms.model.business.SendInfo;
import com.tianqi.rms.model.business.Vip;

/**
 *
 *                #####################################################
 *                #                                                   #
 *                #                       _oo0oo_                     #
 *                #                      o8888888o                    #
 *                #                      88" . "88                    #
 *                #                      (| -_- |)                    #
 *                #                      0\  =  /0                    #
 *                #                    ___/`---'\___                  #
 *                #                  .' \\|     |# '.                 #
 *                #                 / \\|||  :  |||# \                #
 *                #                / _||||| -:- |||||- \              #
 *                #               |   | \\\  -  #/ |   |              #
 *                #               | \_|  ''\---/''  |_/ |             #
 *                #               \  .-\__  '-'  ___/-. /             #
 *                #             ___'. .'  /--.--\  `. .'___           #
 *                #          ."" '<  `.___\_<|>_/___.' >' "".         #
 *                #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 *                #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 *                #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 *                #                       `=---='                     #
 *                #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 *                #                                                   #
 *                #               佛祖保佑         永无BUG              #
 *                #                        						      #
 *                #####################################################
 *								yanglong : Dec 18, 2018 8:52:42 PM 尊上
 */
public abstract class BaseSender implements SmsSender {

	/* (non-Javadoc)
	 * @see com.tianqi.rms.sendFactory.send#Send(java.lang.String)
	 */
	@Override
	@Before(Tx.class)
	public Map<String, Object> Send(String tel,String parasm) {
		Map<String,Object> map = new HashMap<String,Object>();
		SendInfo sendinfo= SendInfo.dao.getCodeNewTime(getType(),tel);
		int verifySate = 1;
		if(sendinfo!=null) {
			Date date = sendinfo.getCreateTime();
			Long vcTime = date.getTime();
			Long nowTime = new Date().getTime();
			Long istime =nowTime-vcTime;
			if(istime<120000L) {
				verifySate = -1;
			}
		}
		if(verifySate == 1) {
			Vip vip = Vip.dao.getVipByUserTel(tel,shop_id); 
			if(vip==null) {
				map.put("result", "-2");
			}else {
				SendInfo smsInfo=new SendInfo();
				smsInfo.setShopId(shop_id);
				smsInfo.setType(getType());
				smsInfo.setTel(tel);
				smsInfo.setParameter(getParams(parasm));
				smsInfo.setCreateTime(new Date());
				if(smsInfo.save()) {
					Map<String, String> data=new HashMap<String, String>();
					data.put("smsId",smsInfo.getId().toString());
					String resJson=HttpKit.post(intFaceUrl, data, null);
					JSONObject json=JSONObject.parseObject(resJson);
					map.put("result", json.getInteger("result"));
					map.put("id", json.getInteger("id"));
					return map;
				}else {
					map.put("result", "-00");
				}
			}
		}else {
			map.put("result", "-1");
		}
		return map;

	}
	
	public abstract int getType();
	public abstract String getParams(String params);

}
